package com.sky.controller.users;

import com.sky.constant.MessageConstant;
import com.sky.dto.FileDTO;
import com.sky.result.Result;
import com.sky.utils.AliOssUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

@RestController
@Slf4j
@Api(tags = "通用接口")
@RequestMapping("/common")
public class CommonController {
    @Autowired
    private AliOssUtil aliOssUtil;
    /**
     * 文件上传
     * @param file
     * @return
     */
    @PostMapping("/upload")
    @ApiOperation("文件上传")
    public Result<String> upload(MultipartFile file){
        log.info("文件上传,{}",file);
        try{
            //获取原始文件名
            String filename = file.getOriginalFilename();
            //获取原始文件名的后缀
            String substring = filename.substring(filename.lastIndexOf("."));
            String newFileName = UUID.randomUUID().toString() + substring;
            String upload = aliOssUtil.upload(file.getBytes(), newFileName);
            return Result.success(upload);
        } catch (Exception e){
            e.printStackTrace();
        }
        return Result.error("文件上传失败!");
    }


//    /**
//     * 文件上传
//     * @param file
//     * @return
//     */
//    @PostMapping("/upload")
//    @ApiOperation("文件上传")
//    public Result<String> upload(@RequestBody FileDTO file){
//        log.info("文件上传,{}",file);
//        System.out.println("文件上传,{}"+file);
//        try{
//            //获取原始文件名
//            String filename = file.getFileName();
//            String file_temp=file.getFileName();
////            获取原始文件名的后缀
//            String substring = filename.substring(filename.lastIndexOf("."));
//            String newFileName = UUID.randomUUID().toString() + substring;
//            String upload = aliOssUtil.upload(file_temp.getBytes(), newFileName);
//            return Result.success(upload);
//        } catch (Exception e){
//            e.printStackTrace();
//        }
//        return Result.error("文件上传失败!");
//    }
}
